<template>
  <el-container class="layout-container">
    <!-- 左侧菜单 -->
    <el-aside width="220px" class="sidebar">
      <div class="logo">
        <span>管理系统</span>
      </div>
      <el-menu :default-active="activeMenu" class="el-menu-vertical" background-color="#304156" text-color="#fff"
        active-text-color="#ffd04b" :unique-opened="true" router>
        <el-menu-item index="/" @click="addTab('首页', '/')">
          <el-icon>
            <HomeFilled />
          </el-icon>
          <span>首页</span>
        </el-menu-item>

        <el-sub-menu index="/user">
          <template #title>
            <el-icon>
              <User />
            </el-icon>
            <span>用户管理</span>
          </template>
          <el-menu-item index="/user/list" @click="addTab('用户列表', '/user/list')">用户列表</el-menu-item>
        </el-sub-menu>

        <el-sub-menu index="/permission">
          <template #title>
            <el-icon>
              <Lock />
            </el-icon>
            <span>权限管理</span>
          </template>
          <el-menu-item index="/permission/list" @click="addTab('权限列表', '/permission/list')">权限列表</el-menu-item>
        </el-sub-menu>
      </el-menu>
    </el-aside>

    <!-- 右侧内容区 -->
    <el-container>
      <!-- 顶部Header区域 -->
      <el-header height="64px" class="header">
        <div class="header-content">
          <!-- 面包屑导航 -->
          <div class="breadcrumb-container">
            <el-breadcrumb separator="/">
              <el-breadcrumb-item v-for="(item, index) in breadcrumbList" :key="index">
                {{ item }}
              </el-breadcrumb-item>
            </el-breadcrumb>
          </div>

          <!-- 右侧登录信息 -->
          <div class="user-info">
            <el-dropdown>
              <div class="user-info-inner">
                <el-avatar :size="36" :src="user.avatar" />
                <div class="user-details">
                  <span class="username">{{ user.name }}</span>
                  <span class="user-role">{{ user.role }}</span>
                </div>
                <el-icon class="dropdown-icon">
                  <ArrowDown />
                </el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item>
                    <el-icon>
                      <User />
                    </el-icon>
                    <span>个人中心</span>
                  </el-dropdown-item>
                  <el-dropdown-item>
                    <el-icon>
                      <Setting />
                    </el-icon>
                    <span>账号设置</span>
                  </el-dropdown-item>
                  <el-dropdown-item divided @click="handleLogout">
                    <el-icon>
                      <SwitchButton />
                    </el-icon>
                    <span>退出登录</span>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </div>
      </el-header>

      <!-- Tab标签区域 -->
      <div class="tabs-container">
        <el-tabs v-model="activeTab" type="border-card" closable @tab-remove="removeTab" @tab-click="clickTab"
          class="demon-tabs">
          <el-tab-pane v-for="item in tabs" :key="item.path" :label="item.title" :name="item.path">
            <!-- 主内容区 -->
            <el-main>
              <router-view v-slot="{ Component }">
                <keep-alive>
                  <component :is="Component" />
                </keep-alive>
              </router-view>
            </el-main>
          </el-tab-pane>
        </el-tabs>
      </div>


    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import {
  HomeFilled,
  User,
  Lock,
  ArrowDown,
  Setting,
  SwitchButton
} from '@element-plus/icons-vue'

const route = useRoute()
const router = useRouter()
const activeMenu = ref(route.path)
const activeTab = ref(route.path)
const tabs = ref([
  {
    title: '首页',
    path: '/'
  }
])

// 用户信息（模拟数据）
const user = ref({
  name: '管理员',
  role: '超级管理员',
  avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png'
})

// 计算面包屑导航
const breadcrumbList = computed(() => {
  return route.meta.breadcrumb || ['首页']
})

// 添加Tab
const addTab = (title, path) => {
  if (!tabs.value.some(tab => tab.path === path)) {
    tabs.value.push({
      title,
      path
    })
  }
  activeTab.value = path
}

// 移除Tab
const removeTab = (targetPath) => {
  const index = tabs.value.findIndex(tab => tab.path === targetPath)
  tabs.value.splice(index, 1)

  // 如果删除的是当前激活的Tab
  if (targetPath === activeTab.value) {
    // 尝试切换到前一个Tab
    const prevTab = tabs.value[index - 1] || tabs.value[0]
    if (prevTab) {
      router.push(prevTab.path)
    } else {
      // 如果没有其他Tab，则跳转到首页
      router.push('/')
    }
  }
}

// 点击Tab
const clickTab = (tab) => {
  router.push(tab.paneName)
}

// 退出登录
const handleLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    // 这里添加实际退出逻辑
    router.push('/login')
    ElMessage.success('退出成功')
  }).catch(() => {
    // 取消退出
  })
}

// 监听路由变化
watch(route, (newRoute) => {
  activeMenu.value = newRoute.path
  activeTab.value = newRoute.path

  // 更新Tab标题
  const currentTab = tabs.value.find(tab => tab.path === newRoute.path)
  if (currentTab && newRoute.meta?.title) {
    currentTab.title = newRoute.meta.title
  }
}, { immediate: true })
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.sidebar {
  background-color: #304156;
  color: #fff;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: bold;
  color: #fff;
  background-color: #2b2f3a;
}

.el-menu-vertical {
  border-right: none;
  height: calc(100% - 60px);
}

.header {
  padding: 0;
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.header-content {
  height: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
}

.breadcrumb-container {
  flex: 1;
}

.user-info {
  margin-left: 20px;
}

.user-info-inner {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 5px 10px;
  transition: background-color 0.3s;
  border-radius: 4px;
}

.user-info-inner:hover {
  background-color: #f5f7fa;
}

.user-details {
  display: flex;
  flex-direction: column;
  margin-left: 10px;
  line-height: 1.4;
}

.username {
  font-size: 14px;
  font-weight: 500;
}

.user-role {
  font-size: 12px;
  color: #909399;
}

.dropdown-icon {
  margin-left: 5px;
  color: #909399;
  font-size: 12px;
}

.tabs-container {
  padding: 0 20px;
  background-color: #fff;
}

.el-main {
  padding: 20px;
  background-color: #f0f2f5;
}

/* 面包屑样式 */
.el-breadcrumb {
  line-height: 50px;
}

.el-breadcrumb :deep(.el-breadcrumb__inner) {
  font-weight: normal;
}

.el-breadcrumb :deep(.el-breadcrumb__item:last-child .el-breadcrumb__inner) {
  font-weight: bold;
  color: #303133;
}

/* Tab样式 */
:deep(.el-tabs__header) {
  margin: 0;
}

:deep(.el-tabs__item) {
  height: 40px;
  line-height: 40px;
  font-size: 14px;
}

:deep(.el-tabs__item.is-active) {
  background-color: #f0f7ff;
  border-bottom-color: #409eff;
  color: #409eff;
  font-weight: bold;
}

/* 下拉菜单样式 */
:deep(.el-dropdown-menu__item) {
  display: flex;
  align-items: center;
}

:deep(.el-dropdown-menu__item .el-icon) {
  margin-right: 8px;
  font-size: 16px;
}

/* 绿色Tab样式 */
:deep(.green-tabs .el-tabs__header) {
  margin: 0;
  border-bottom: none;
  background: #f5f5f5;
  padding: 8px 10px 0;
  height: 40px;
  box-sizing: border-box;
}

:deep(.green-tabs .el-tabs__nav) {
  border: none;
  display: flex;
  gap: 4px;
}

:deep(.green-tabs .el-tabs__item) {
  height: 32px;
  line-height: 32px;
  font-size: 13px;
  color: #555;
  border: 1px solid #ccc;
  border-bottom: none;
  border-radius: 6px 6px 0 0;
  margin-right: 0;
  background: #f0f0f0;
  padding: 0 16px;
  position: relative;
  transition: all 0.2s ease;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

:deep(.green-tabs .el-tabs__item:hover) {
  background: #e8e8e8;
}

:deep(.green-tabs .el-tabs__item.is-active) {
  background: #13ce66;
  color: white;
  border-color: #13ce66;
  z-index: 1;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

:deep(.green-tabs .el-tabs__item.is-active:after) {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 0;
  right: 0;
  height: 1px;
  background: #13ce66;
}

:deep(.green-tabs .el-tabs__item .el-icon-close) {
  width: 14px;
  height: 14px;
  line-height: 14px;
  border-radius: 50%;
  color: #777;
  transition: all 0.2s;
  margin-left: 6px;
}

:deep(.green-tabs .el-tabs__item .el-icon-close:hover) {
  background-color: rgba(255, 255, 255, 0.3);
  color: white;
}

:deep(.green-tabs .el-tabs__item.is-active .el-icon-close) {
  color: rgba(255, 255, 255, 0.7);
}

:deep(.green-tabs .el-tabs__item.is-active .el-icon-close:hover) {
  background-color: rgba(255, 255, 255, 0.2);
  color: white;
}

:deep(.green-tabs .el-tabs__nav-next),
:deep(.green-tabs .el-tabs__nav-prev) {
  height: 32px;
  line-height: 32px;
  color: #777;
  background: #f0f0f0;
  border-radius: 4px;
  border: 1px solid #ccc;
}

:deep(.green-tabs .el-tabs__nav-next:hover),
:deep(.green-tabs .el-tabs__nav-prev:hover) {
  background: #e0e0e0;
  color: #333;
}
</style>